package com.noriental.okcalls.config;

import com.alibaba.fastjson.JSON;
import com.noriental.okcalls.wechat.callback.WxMsgDispatchCenter;
import com.noriental.okcalls.wechat.callback.dispatch.WxWxMsgDefaultDispatcher;
import com.noriental.okcalls.wechat.config.ApiConfig;
import com.noriental.okcalls.wechat.config.WxApiConfigInstance;
import com.noriental.okcalls.wechat.msg.in.InTextMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/***
 * *   ____  ___________  ___________           ________   ____  __.  _____ _____.___.
 * *   \   \/  /\______ \ \_   _____/           \_____  \ |    |/ _| /  _  \\__  |   |
 * *    \     /  |    |  \ |    __)     ______   /   |   \|      <  /  /_\  \/   |   |
 * *    /     \  |    `   \|     \     /_____/  /    |    \    |  \/    |    \____   |
 * *   /___/\  \/_______  /\___  /              \_______  /____|__ \____|__  / ______|
 * *    	 \_/        \/     \/                       \/        \/       \/\/
 * *
 * *   功能描述：
 * *
 * *   @DATE    2020-10-12
 * *   @AUTHOR  qiyubin
 ***/
@Slf4j
@Configuration
public class CustomWxConfig {

    @Autowired
    WxMsgDispatchCenter demoDispatchCenter;

    /**
     * @throws Exception
     */
    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        log.info("start loading");

        ApiConfig wxConfig = new ApiConfig();
        WxApiConfigInstance.devMode = false;
        wxConfig.setAppId("wx5f0e1f1c8446efaf");
        wxConfig.setAppSecret("ef0488561ee140d7c8b507da7b30a84b");
        wxConfig.setEncodingAesKey("");
        wxConfig.setToken("");
        wxConfig.setEncryptMessage(false);
        WxApiConfigInstance.apiConfig = wxConfig;

        log.info("wxConfig is {}", JSON.toJSONString(WxApiConfigInstance.apiConfig));

    }

    //instance set


    //call back define
    @Bean
    WxMsgDispatchCenter demoDispatchCenter() {
        log.info("start loading custom dispatch center");

        WxMsgDispatchCenter wxMsgDispatchCenter = new WxMsgDispatchCenter();
        wxMsgDispatchCenter.setWxMsgDispatcher(new MyWxDispatcher());
        return wxMsgDispatchCenter;
    }

    class MyWxDispatcher extends WxWxMsgDefaultDispatcher {
        @Override
        public void processInTextMsg(InTextMsg inTextMsg) {
            //todo 文本消息处理
            super.processInTextMsg(inTextMsg);
        }
    }

}
